package com.ad.platform.service.impl;

import com.ad.payload.dto.LoginDto;
import com.ad.payload.util.JwtUtil;
import com.ad.payload.vo.LoginVo;
import com.ad.platform.common.interceptor.RequestHolder;
import com.ad.platform.common.utils.BeanUtil;
import com.ad.platform.common.utils.UuidUtil;
import com.ad.platform.mapper.WxUserMapper;
import com.ad.platform.model.WxUser;
import com.ad.platform.service.IPfLoginService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author:dxgong
 * @日期 2021/3/21 15:58
 * @注释
 */
@Service
@Slf4j
public class PfLoginServiceImpl implements IPfLoginService{

    @Autowired
    private WxUserMapper wxUserMapper;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 添加微信用户
     *
     * @param loginDto
     */
    @Override
    public LoginVo insertWxUser(LoginDto loginDto) {
        // 校验unionID是否存在
        System.out.println("-============== " + RequestHolder.getCurrentUser());
        QueryWrapper<WxUser> queryWrapper = new QueryWrapper();
        queryWrapper.eq("union_id", loginDto.getUnionId());
        //queryWrapper.eq("open_id", loginDto.getOpenId());

        List<WxUser> wxUsers = this.wxUserMapper.selectList(queryWrapper);

        WxUser wxUser = new WxUser();
        BeanUtil.copyProperties(loginDto, wxUser);

        wxUser.setSex(Integer.valueOf(loginDto.getSex()));
        wxUser.setCity(loginDto.getCity());
        wxUser.setCountry(loginDto.getCountry());
        wxUser.setProvince(loginDto.getProvince());
        wxUser.setImg(loginDto.getImg());
        wxUser.setWechatName(loginDto.getNickName());


        LoginVo loginVo = new LoginVo();
        if (!wxUsers.isEmpty()) {
            // 根据unionId更新时间
            wxUser.setUpdateTime(new Date());
            this.wxUserMapper.update(wxUser, queryWrapper);

            // 刷新jwtToken时间
            Map<String, Object> map = new HashMap<>();
            map.put("uuid", wxUsers.get(0).getUuid());
            map.put("id", wxUsers.get(0).getId());
            map.put("weChatName", wxUsers.get(0).getWechatName());
            String jwt = jwtUtil.createJWT(map);
            loginVo.setJwtToken(jwt);
            loginVo.setUuid(wxUsers.get(0).getUuid());
            return loginVo;
        }

        // 新增
        wxUser.setUnionId(loginDto.getUnionId());
        if(loginDto.getType() == 0){
            // 公众号注册
            log.info("======== 公众号注册 =======");
            wxUser.setServiceOpenId(loginDto.getOpenId());
        } else {
            // 小指令注册
            log.info("======== 小指令注册 =======");
            wxUser.setOpenId(loginDto.getOpenId());
        }
        String uuid = UuidUtil.get32UUID();
        wxUser.setUuid(uuid);
        this.wxUserMapper.insert(wxUser);

        Map<String, Object> map = new HashMap<>();
        map.put("uuid", uuid);
        map.put("id", wxUser.getId());
        map.put("weChatName", wxUser.getWechatName());
        String jwt = jwtUtil.createJWT(map);
        loginVo.setJwtToken(jwt);
        loginVo.setUuid(uuid);
        return loginVo;
    }
}
